package com.xinrui.freemarker;

import com.xinrui.pojo.Person;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class FreemarkerDemo {
    public static void main(String[] args) throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.getVersion());
        configuration.setDirectoryForTemplateLoading(new File("D://ftl"));
        configuration.setURLEscapingCharset("utf-8");

        Template template = configuration.getTemplate("test.ftl");
        Map<String, Object> map = new HashMap<>();
        map.put("name", "君莫笑");
        map.put("message", "欢迎来到快乐星球");
        map.put("info", new Person("18570241587", "北京"));
        map.put("success", true);

        ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("name", "苹果");
        map1.put("price", 5.0);

        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("name", "香蕉");
        map2.put("price", 4.5);

        HashMap<String, Object> map3 = new HashMap<>();
        map3.put("name", "西瓜");
        map3.put("price", 3.6);

        arrayList.add(map1);
        arrayList.add(map2);
        arrayList.add(map3);
        map.put("goodsList", arrayList);

        FileWriter writer = new FileWriter(new File("D://ftl//test.html"));
        template.process(map, writer);
        writer.close();
    }
}
